package com.nuanshui.heatedloan.task.job;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;

import com.nuanshui.heatedloan.task.constant.TaskGlobalParams;
import com.nuanshui.heatedloan.task.facade.TaskMessageSender;
import com.nuanshui.heatedloan.task.facade.bean.JobParams;
import com.nuanshui.heatedloan.task.util.CommonUtil;

/**
 * 所有定时任务执行时的入口
 * @param 
 * @author guohao
 * @date 2017年2月13日 上午10:07:33
 * @since $
 */
public class QuartzJobBean implements Job {

	private Logger logger = LoggerFactory.getLogger(QuartzJobBean.class);
	@Autowired
	private TaskMessageSender taskMessageSender;
	
	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {	 
		 JobDataMap map =  context.getJobDetail().getJobDataMap();
		 JobParams jobParams =  (JobParams) map.get("job_params");
		 String jobName = jobParams.getJobName();
		 String jobGroup = jobParams.getJobGroup();
		 String code = CommonUtil.generateJobCode(jobParams);
		 jobParams.setJobCode(code);
		 MDC.put(TaskGlobalParams.JOB_LOG_CODE_KEY,code);
		 logger.info(jobGroup+"_"+jobName+" 开始执行定时任务");
		 String value = CommonUtil.generateMessageQueueName(jobGroup, jobName);
		 taskMessageSender.send(value, jobParams);
		 logger.info(jobGroup+"_"+jobName+"  定时任务发送成功");
		 MDC.remove(TaskGlobalParams.JOB_LOG_CODE_KEY);

	}

}
